﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DemoApplication1
{
    using DbfDotNet;

    public partial class IndividualsForm : Form
    {
        DbfTable<Individual> individuals;

        const int ADD_INDIVIDUALS_COUNT = 10000;

        public IndividualsForm()
        {
            foreach (var fi in new System.IO.DirectoryInfo(".").GetFiles("*.ndx"))
            {
                fi.Delete();
            }

            InitializeComponent();
            individuals = new DbfTable<Individual>(@"individuals.dbf", Encoding.ASCII, DbfVersion.dBaseIV);

            tableView1.DbfTable = individuals;
            this.addToolStripMenuItem.Text = string.Format("Add {0:0,0} individuals", ADD_INDIVIDUALS_COUNT);
            RefreshBar();
        }

        private void quitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void RefreshBar()
        {
            toolStripStatusLabel1.Text = string.Format("Ready ({0} records in database).", individuals.RecordCount);
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            individuals.Close();
        }

        private void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int k = individuals.RecordCount;
            for (int i = 0; i < ADD_INDIVIDUALS_COUNT; i++)
            {
                var newIndiv = individuals.NewRecord();
                bool male = RandomData.RandomBool();
                newIndiv.FIRSTNAME = (male ? RandomData.RandomMaleFirstname() : RandomData.RandomFemaleFirstname());
                string middleName = (male ? RandomData.RandomMaleFirstname() : RandomData.RandomFemaleFirstname());
                if (middleName != newIndiv.FIRSTNAME)
                    newIndiv.MIDDLENAME = middleName;
                newIndiv.LASTNAME = RandomData.RandomLastName();
                newIndiv.STATE = RandomData.RandomState();
                newIndiv.DOB = RandomData.RandomDob();
                k++;
                newIndiv.SaveChanges();
            }
            RefreshBar();
            tableView1.RefreshGrid();
        }

        private void clearFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void tableView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Individual individual = null;

            if (tableView1.GetRow((UInt32)tableView1.FocusedItem.Index, ref individual))
            {
                var detailForm = new DetailForm();
                detailForm.ReadIndividual(individual);
                if (detailForm.ShowDialog() == DialogResult.OK)
                {
                    detailForm.WriteIndividual(individual);
                    individual.SaveChanges();
                }
            }
        }

        private void clearFileToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            individuals.EmptyTable();
            tableView1.RefreshGrid();
            RefreshBar();
        }

    }
}
